/*
 * Copyright @ 苏州瑞云信息技术有限公司 All rights reserved.
 */
package cn.com.recloud.bbs.controller;


import cn.com.recloud.bbs.constant.MetaOptionConstant;
import cn.com.recloud.bbs.dto.CampaignAnalysisDTO;
import cn.com.recloud.bbs.entity.bbs_campaign;
import cn.com.recloud.bbs.service.CampaignService;
import cn.com.recloud.bbs.util.FileUploadUtil;
import cn.com.recloud.bbs.vo.CampaignAppVO;
import cn.com.recloud.bbs.vo.CampaignVO;
import cn.com.recloud.core.util.Result;
import cn.com.recloud.one.spring.controller.BaseController;
import cn.hutool.core.util.ObjectUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author kim dong
 * @date 2020-06-08
 */
@RestController
@Api(tags = "活动相关接口")
@RequestMapping("/api/bbs/campaign")
public class CampaignController extends BaseController<bbs_campaign, CampaignService> {
    @Autowired
    private CampaignService campaignService;

    @Autowired
    private FileUploadUtil fileUploadUtil;

    @PostMapping("/savecampaign")
    @ApiOperation("pc保存活动")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "campaign", value = "活动的实体"),
            @ApiImplicitParam(name = "file", value = "活动封面图片"),
            @ApiImplicitParam(name = "stfile", value = "活动启动页图片"),
    })
    public Result saveCampaign(
                               @ModelAttribute bbs_campaign campaign,
                               @RequestParam(value = "file", required = false) MultipartFile file,
                               @RequestParam(value = "stfile", required = false) MultipartFile stfile) {
        if (ObjectUtil.isNotEmpty(file)) {
            // 有封面页图片，则路径存入数据库中
            campaign.setAvatar(fileUploadUtil.upload(file));
        }
        if (ObjectUtil.isNotEmpty(stfile)) {
            // 有启动页图片，则路径存入数据库中
            campaign.setStart_page(fileUploadUtil.upload(stfile));
        }
        String campaignId = campaignService.saveCampaign(campaign);
        return new Result(campaignId);
    }

    @PostMapping("/updatecampaign")
    @ApiOperation("pc更新活动")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "campaign", value = "活动的实体"),
            @ApiImplicitParam(name = "flag", value = "0 启动页图片不存在，1 启动页图片存在"),
            @ApiImplicitParam(name = "file", value = "活动封面图片"),
            @ApiImplicitParam(name = "stfile", value = "活动启动页图片"),
    })
    public Result updateCampaign(
                                 @ModelAttribute bbs_campaign campaign,
                                 @RequestParam(value = "flag", required = false) String flag,
                                 @RequestParam(value = "file", required = false) MultipartFile file,
                                 @RequestParam(value = "stfile", required = false) MultipartFile stfile) {
        if (ObjectUtil.isNotEmpty(file)) {
            campaign.setAvatar(fileUploadUtil.upload(file));
        }
        if (ObjectUtil.isNotEmpty(stfile)) {
            // 有启动页图片，则路径存入数据库中
            campaign.setStart_page(fileUploadUtil.upload(stfile));
        }
        // 这个判断是解决再更新活动时，启动页图片删除了是一个null，不更改还是一个null，会导致要删除图片时删除不掉图片的情况
        if (!campaign.getIs_display_start_page() && "1".equals(flag)) {
            // 删除启动页图片的更新操作
            campaignService.updateStById(campaign);
        } else {
            campaignService.updateById(campaign);
        }
        return Result.SUCCESS;
    }

    @PostMapping("/releasecampaign")
    @ApiOperation("pc发布活动")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "campaign", value = "活动的实体"),
            @ApiImplicitParam(name = "flag", value = "0 启动页图片不存在，1 启动页图片存在"),
            @ApiImplicitParam(name = "file", value = "活动封面图片"),
            @ApiImplicitParam(name = "stfile", value = "活动启动页图片"),
    })
    public Result releaseCampaign(@ModelAttribute bbs_campaign campaign,
                                  @RequestParam(value = "flag", required = false) String flag,
                                  @RequestParam(value = "file", required = false) MultipartFile file,
                                  @RequestParam(value = "stfile", required = false) MultipartFile stfile) {
        if (ObjectUtil.isNotEmpty(file)) {
            campaign.setAvatar(fileUploadUtil.upload(file));
        }
        if (ObjectUtil.isNotEmpty(stfile)) {
            // 有启动页图片，则路径存入数据库中
            campaign.setStart_page(fileUploadUtil.upload(stfile));
        }
        String campaignId = campaignService.releaseCampaign(flag, campaign);
        return new Result(campaignId);
    }

    @GetMapping("getcampaignvo/{id}")
    @ApiOperation("pc获取活动详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "活动的id")
    })
    public Result getCampaignVO(@PathVariable("id") String id) {
        CampaignVO campaign = campaignService.getCampaignVO(id);
        return new Result(campaign);
    }

    @PostMapping("activaterelease/{id}")
    @ApiOperation("发布活动")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "活动的id")
    })
    public Result activateRelease(@PathVariable("id") String id) {
        campaignService.activateRelease(id);
        return Result.SUCCESS;
    }

    @PostMapping("deactivaterelease/{id}")
    @ApiOperation("取消发布活动")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "活动的id")
    })
    public Result deactivateRelease(@PathVariable("id") String id) {
        campaignService.deactivateRelease(id);
        return Result.SUCCESS;
    }

    @PostMapping("releasebatch")
    @ApiOperation("列表页批量发布")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "idList", value = "活动的id集合")
    })
    public Result releaseBatch(@RequestBody List<String> idList) {
        boolean count = campaignService.releaseBatch(idList, MetaOptionConstant.RELEASE_STAUS_ABLE);
        return new Result(count);
    }

    @PostMapping("dereleasebatch")
    @ApiOperation("列表页批量取消发布")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "idList", value = "活动的id集合")
    })
    public Result dereleaseBatch(@RequestBody List<String> idList) {
        boolean count = campaignService.releaseBatch(idList, MetaOptionConstant.RELEASE_STAUS_DISABLED);
        return new Result(count);
    }

    @PostMapping("listcampaignanalysis")
    @ApiOperation("活动数据分析统计")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "campaign", value = "活动的实体")
    })
    public Result listCampaignAnalysis(@RequestBody CampaignAnalysisDTO campaign) {
        Map map = campaignService.listCampaignAnalysisTwo(campaign);
        return new Result(map);
    }

    @PostMapping("exportcampaignanalysis")
    @ApiOperation("导出活动分析数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "response", value = "响应对象"),
            @ApiImplicitParam(name = "campaign", value = "活动的实体")
    })
    public void exportCampaignAnalysis(HttpServletResponse response, @RequestBody CampaignAnalysisDTO campaign) {
        campaignService.exportCampaignAnalysis(campaign, response);
    }

    @GetMapping("/listappcampaign")
    @ApiOperation("app活动列表")
    public Result listAppCampaign(String name,
                                  @RequestParam(required = false, defaultValue = "1") Integer pageIndex,
                                  @RequestParam(required = false, defaultValue = "15") Integer pageSize) {
        List<CampaignAppVO> list = campaignService.findAppCampaignList(name, pageIndex, pageSize);
        return new Result(list);
    }


    @GetMapping({"/page/startup/{code}/{time}", "/page/startup/{code}", "/page/startup"})
    @ApiOperation(value = "启动页判断是否有广告")
    public Result isLogo(@PathVariable(name = "code", required = false) String code, @PathVariable(name = "time", required = false) String time) {
        Map map = campaignService.findAllShowCampaign(time, code);
        return new Result(map);
    }
}

